package com.yingli.storage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.yingli.storage.net.NetRequestActivity;
import com.yingli.storage.provider.ContactActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StringBuilder builder = new StringBuilder();
        StringBuffer buffer = new StringBuffer();
        for (int i=0;i<30;i++){
            final int j= i ;
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    builder.append(","+j);
                    buffer.append(","+j);
                }
            }.start();
        }
        new Handler(getMainLooper()).postDelayed(()->{
            Log.i("StringBuilderResult",builder.toString());
            Log.i("StringBuilderResult",buffer.toString());
        },2000);

        //第一个参数是保存的sharedPreferences的文件名称
        SharedPreferences preferences = getSharedPreferences("settings",MODE_PRIVATE);
        String prefs = preferences.getString("is_open","error");
//        System.out.println(prefs);
        Log.i("sharedValue",prefs);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("is_open","true");
        editor.putInt("volume",10);
        //提交后，无需确认存储结果
        editor.apply();
        //提交后，可以知道是否存储成功，返回boolean值
//        editor.commit();
        prefs = preferences.getString("is_open","error");
//        System.out.println(prefs);
        Log.i("sharedValue",prefs);
        findViewById(R.id.btn_provider).setOnClickListener(v->{
            startActivity(new Intent(this, ContactActivity.class));
        });

        findViewById(R.id.btn_network).setOnClickListener(v->{
            startActivity(new Intent(this, NetRequestActivity.class));
        });
    }
}